package com.link2room.mns;

import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudQueue;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.model.Message;
import com.aliyun.mns.model.QueueMeta;
import com.link2room.aeriabots.constant.AeriabotsMnsConstant;
import com.link2room.blaster.constant.BlasterMnsConstant;
import com.link2room.common.constant.MnsConstant;
import com.link2room.common.dto.result.base.MnsParamDto;
import com.link2room.common.mns.proxy.MessageServiceProxy;
import com.link2room.ironhide.constant.IronhideMnsConstant;
import com.link2room.jetfire.constant.JetfireMnsConstant;
import com.link2room.optimus.constant.OptimusMnsConstant;
import com.link2room.quintesson.constant.QuintessonMnsConstant;
import com.link2room.rodimus.constant.RodimusMnsConstant;
import com.link2room.soundwave.constant.SoundwaveMnsConstant;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created by LN on 2016/9/19.
 */
public class Topic {
    //dev

    private static String accessKeyId = "LTAIQBnVVpx9epab";
    private static String accessKeySecret = "4KonmhfBhMb6g24OyDj7KULC3YMqOY";
    private static String accountEndpoint = "http://1729952071197664.mns.cn-shenzhen.aliyuncs.com/" ;


    //link2room prod
    /*private static String accessKeyId = "LTAIQBnVVpx9epab";
    private static String accessKeySecret = "4KonmhfBhMb6g24OyDj7KULC3YMqOY";
    private static String accountEndpoint = "http://1729952071197664.mns.cn-hangzhou.aliyuncs.com/" ;*/
    //link2room test
   /*private static String accessKeyId = "LTAIQBnVVpx9epab";
    private static String accessKeySecret = "4KonmhfBhMb6g24OyDj7KULC3YMqOY";
    private static String accountEndpoint = "http://1729952071197664.mns.cn-shanghai.aliyuncs.com/" ;
*/
    public static void main(String[] args) throws Exception {
//        createTopicAndSub();
//        createQ(MnsConstant.QUEUE_MSG_LOG);
//        createQ(MnsConstant.QUEUE_PAYMENT_LOG);
//        createQ(MnsConstant.QUEUE_PMS_MEMBER);
//        createQ(MnsConstant.QUEUE_MSG_TO_SEND);
//        createQ("MSG-TO-LOGGER");
//        createQ("MSG-TO-ORDER-STA");
//        createQ(QUEUE_MSG_TO_BRANDWISDOM_GSS);


//        popMsg();
//        createQ(MnsConstant.QUEUE_ORDER_CONFIRMSTA);
//        createQ(MnsConstant.QUEUE_MSG_ORDER_AERIABOTS);
//        createQ(MnsConstant.QUEUE_ORDER_RODIMUS);
//        createQ(MnsConstant.QUEUE_ORDER_OPTIMUS);
       //createQ(SoundwaveMnsConstant.QUEUE_ORDER_SOUNDWAVE);


        //createTopicAndSub(MnsConstant.MNS_TOPIC_ORDER);
    }

    /**
     * 创建队列
     */
    private static void createAllQ(String qName){
        List<MnsParamDto> mnsParamDtos=getParamList();
        for(MnsParamDto mnsParamDto:mnsParamDtos){
            CloudAccount account = new CloudAccount(mnsParamDto.getAccessKeyId(),mnsParamDto.getAccessKeySecret(),mnsParamDto.getAccountEndpoint());
            MNSClient client = account.getMNSClient();
            QueueMeta qMeta = new QueueMeta();
            qMeta.setQueueName(qName);
            qMeta.setPollingWaitSeconds(30);//use long polling when queue is empty.
            CloudQueue cQueue = client.createQueue(qMeta);
        }
    }

    private static List<MnsParamDto> getParamList(){
        //shenzhen-DEV
        MnsParamDto p2=new MnsParamDto();
        p2.setAccessKeyId("LTAIQBnVVpx9epab");
        p2.setAccessKeySecret("4KonmhfBhMb6g24OyDj7KULC3YMqOY");
        p2.setAccountEndpoint("http://1729952071197664.mns.cn-shenzhen.aliyuncs.com/");
        //PROD
        MnsParamDto p3=new MnsParamDto();
        p3.setAccessKeyId("LTAIQBnVVpx9epab");
        p3.setAccessKeySecret("4KonmhfBhMb6g24OyDj7KULC3YMqOY");
        p3.setAccountEndpoint("http://1729952071197664.mns.cn-hangzhou.aliyuncs.com/");
        //TEST
        MnsParamDto p4=new MnsParamDto();
        p4.setAccessKeyId("LTAIQBnVVpx9epab");
        p4.setAccessKeySecret("4KonmhfBhMb6g24OyDj7KULC3YMqOY");
        p4.setAccountEndpoint("http://1729952071197664.mns.cn-shanghai.aliyuncs.com/");
        List<MnsParamDto> mnsParamDtos=new ArrayList<>();
        mnsParamDtos.add(p2);
        mnsParamDtos.add(p3);
        mnsParamDtos.add(p4);
        return mnsParamDtos;
    }

    /**
     * 创建队列
     */
    private static void createQ(String qName){
        CloudAccount account = new CloudAccount(accessKeyId,accessKeySecret,accountEndpoint);
        MNSClient client = account.getMNSClient();
        QueueMeta qMeta = new QueueMeta();
        qMeta.setQueueName(qName);
        qMeta.setPollingWaitSeconds(30);//use long polling when queue is empty.
        CloudQueue cQueue = client.createQueue(qMeta);
    }

    private static String topicName = "TOPIC-ORDER";
    /**
     * 创建topic并关联相关队列
     */
    private static void createTopicAndSub(String topicName) throws Exception {
        MessageServiceProxy msp = new MessageServiceProxy(accessKeyId,accessKeySecret,accountEndpoint);
        Vector<String> qList = new Vector<String>();
        /*qList.add(MnsConstant.QUEUE_SUB_ORDER_OPTIMUS);
        qList.add(MnsConstant.QUEUE_SUB_ORDER_RODIMUS);
        qList.add(MnsConstant.QUEUE_SUB_ORDER_AERIABOTS);*/
        msp.createPullTopic(topicName,qList);
    }

    /**
     * 发送订阅消息
     *
     */
    private static void putMsg2Topic(){
        MessageServiceProxy msp = new MessageServiceProxy(accessKeyId,accessKeySecret,accountEndpoint);
        msp.publishMessageToTopic(topicName,"测试是生死hi十四号四号");
    }

    /**
     * 收消息
     */
    private static void popMsg(){
        MessageServiceProxy msp = new MessageServiceProxy(accessKeyId,accessKeySecret,accountEndpoint);
        Message msg = msp.popMessage(MnsConstant.QUEUE_BATCH_TASK, 20);
        System.out.println(msg.getMessageBody());
    }

}




